package com.example.work.impor;



import com.example.work.check.AssetComparisonService;
import com.example.work.check.AssetImportService;
import com.example.work.check.ComparisonResult;

import java.io.BufferedWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 数据清洗器
 */
public class YourCustomRunner {
/*    public static void main(String[] args) {
        String excelDirectory = "D:\\tool";

        try {
// 创建导入服务
            AssetImportService importService = new AssetImportService();

// 导入Excel数据
            importService.importFromDirectory(excelDirectory);

// 执行数据比对
            ComparisonResult result = importService.compareWithDatabase();

// 查看比对结果
            System.out.println(result.generateDetailedReport());

            if (!result.getInsertRecords().isEmpty()) {
                AssetComparisonService comparisonService = new AssetComparisonService();
//                List<String> sqlStatements = comparisonService.generateInsertSqlStatements(result);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/



    public static void main(String[] args) {
        String excelDirectory = "D:\\tool";
        try {
            // 创建导入服务
            AssetImportService importService = new AssetImportService();
            // 导入Excel数据
            importService.importFromDirectory(excelDirectory);
            // 执行数据比对
            ComparisonResult result = importService.compareWithDatabase();

            // 创建输出目录（如果不存在）
            Path outputDir = Paths.get(excelDirectory, "sql_output");
            if (!Files.exists(outputDir)) {
                Files.createDirectories(outputDir);
            }

            // 生成带时间戳的SQL文件名
            String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"));
            String sqlFileName = "asset_import_" + timestamp + ".sql";
            Path sqlFilePath = outputDir.resolve(sqlFileName);

            // 写入SQL文件
            try (BufferedWriter writer = Files.newBufferedWriter(sqlFilePath)) {
                // 写入文件头注释
                writer.write("-- 资产导入SQL脚本\n");
                writer.write("-- 生成时间: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "\n\n");

                // 写入比对结果摘要
                writer.write("-- 比对结果摘要:\n");
                writer.write("-- 新增记录数: " + result.getInsertRecords().size() + "\n");
                // 如果有插入记录，生成并写入SQL语句
                if (!result.getInsertRecords().isEmpty()) {
                    AssetComparisonService comparisonService = new AssetComparisonService();
                    List<String> sqlStatements = comparisonService.generateInsertSqlStatements(result);

                    writer.write("-- 新增记录SQL语句:\n");
                    for (String sql : sqlStatements) {
                        writer.write(sql + "\n");
                    }
                } else {
                    writer.write("-- 没有需要新增的记录\n");
                }

                List<ComparisonResult.InvalidRecord> invalidRecords = result.getInvalidRecords();
                if (!invalidRecords.isEmpty()) {
                    for (ComparisonResult.InvalidRecord invalidRecord : invalidRecords) {
                        writer.write("-- 无效记录原因: " + invalidRecord.getReason() + "\n");
                        writer.write("-- 原始记录: " + invalidRecord.getOriginalRecord() + "\n");

                    }
                }

                System.out.println("SQL脚本已成功生成: " + sqlFilePath.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}